package com.example.lenovo.a203_yhy_ordering_3.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.lenovo.a203_yhy_ordering_3.R;
//import com.example.lenovo.a203_yhy_ordering_3.adapter.FoodInfoAdapter;
import com.example.lenovo.a203_yhy_ordering_3.adapter.FoodCommentAdapter;
import com.example.lenovo.a203_yhy_ordering_3.adapter.FoodListAdapter;
import com.example.lenovo.a203_yhy_ordering_3.beans.FoodCommentBean;
import com.example.lenovo.a203_yhy_ordering_3.beans.FoodInfoBean;
import com.example.lenovo.a203_yhy_ordering_3.beans.FoodListBean;
import com.example.lenovo.a203_yhy_ordering_3.beans.IsCollectBean;
import com.example.lenovo.a203_yhy_ordering_3.beans.UserCollectFoodBean;
import com.example.lenovo.a203_yhy_ordering_3.listener.FoodCommentListener;
import com.example.lenovo.a203_yhy_ordering_3.listener.FoodInfoListener;
import com.example.lenovo.a203_yhy_ordering_3.listener.FoodListListener;
import com.example.lenovo.a203_yhy_ordering_3.listener.IsCollectListener;
import com.example.lenovo.a203_yhy_ordering_3.listener.UserCollectFoodListener;
import com.example.lenovo.a203_yhy_ordering_3.model.FoodCommentModel;
import com.example.lenovo.a203_yhy_ordering_3.model.FoodInfoModel;
import com.example.lenovo.a203_yhy_ordering_3.model.FoodListModel;
import com.example.lenovo.a203_yhy_ordering_3.model.IsCollectModel;
import com.example.lenovo.a203_yhy_ordering_3.model.UserCollectFoodModel;
import com.squareup.picasso.Picasso;

import java.util.List;

public class FoodInfoActivity extends AppCompatActivity {
    //菜品详情
    private ImageView imageView;
    private TextView textView4,textView5,textView6;
    private Button button;
    private int food_id;
    private int price;
    private String foodname,intro,pic;
    //评论
    private int user_id;
    private List<FoodCommentBean> list;
    private RecyclerView recyclerView;
    private FoodCommentAdapter foodCommentAdapter;
    private SharedPreferences sharedPreferences;
    //收藏
    private ImageView imageView3;
    private TextView textView8;
    private boolean isCollect;
    //顶栏
    private ImageView imageView8;
    private TextView textView3;

    private FoodCommentListener foodCommentsListener=new FoodCommentListener() {
        @Override
        public void onResponse(List<FoodCommentBean> foodCommentBeans) {

            list = foodCommentBeans;
            foodCommentAdapter.setList(list);

        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(FoodInfoActivity.this,msg,Toast.LENGTH_LONG).show();
        }
    };

    //店铺列表
    public FoodInfoListener foodInfoListener = new FoodInfoListener() {

        @Override
        public void onResponse(FoodInfoBean foodinfo) {
            textView3.setText(foodinfo.getFoodname());
            textView4.setText(foodinfo.getFoodname());
            textView5.setText(foodinfo.getPrice()+"元");
            textView6.setText(foodinfo.getIntro());
            Picasso.get()
                    .load("http://172.24.10.175:8080/foodService"+foodinfo.getPic())
                    .into(imageView);
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(getApplicationContext(),"获取菜品详情失败"+msg,Toast.LENGTH_SHORT).show();
        }
    };


            @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_food_info);

            //收藏
            imageView3=findViewById(R.id.imageView3);
            textView8=findViewById(R.id.textView8);
            //菜品详情信息
            imageView=findViewById(R.id.imageView);
            textView4=findViewById(R.id.textView4);
            textView5=findViewById(R.id.textView5);
            textView6=findViewById(R.id.textView6);
            //顶栏
            textView3=findViewById(R.id.textView3);
            //购买
            button=findViewById(R.id.button);
             //返回
            imageView8=findViewById(R.id.imageView8);

            food_id=getIntent().getIntExtra("food_id",0);
            sharedPreferences=getSharedPreferences("user",MODE_PRIVATE);
            user_id=Integer.parseInt(sharedPreferences.getString("userid","获取userid失败"));

            FoodCommentModel foodCommentModel=new FoodCommentModel();
            foodCommentModel.getFoodCommentResult(food_id,foodCommentsListener);

             FoodInfoModel foodInfoModel = new FoodInfoModel();
             foodInfoModel.getFoodInfoResult(food_id,foodInfoListener);

             initRecyclerView();
             isCollect();
             initEVents();//处理按钮点击事件

        }

    private void initRecyclerView() {
        recyclerView=findViewById(R.id.recyclerview_comment);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        foodCommentAdapter=new FoodCommentAdapter();
        recyclerView.setAdapter(foodCommentAdapter);
    }

    private void initEVents() {
        //购买
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        //收藏
        imageView3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                userCollectFood();
            }
        });
        //返回
        imageView8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void isCollect(){
        IsCollectModel isCollectModel=new IsCollectModel();
        isCollectModel.getIsCollectResult(user_id,food_id,1,isCollectListener);
    }

    private void userCollectFood(){
        UserCollectFoodModel userCollectFoodModel = new UserCollectFoodModel();
        userCollectFoodModel.getUserCollectFoodResult(user_id,food_id,userCollectFoodListener);
    }


    private UserCollectFoodListener userCollectFoodListener  =new UserCollectFoodListener() {
        @Override
        public void onResponse(UserCollectFoodBean userCollectFoodBean) {
            if(userCollectFoodBean.getSuccess().equals("0")){
                Toast.makeText(FoodInfoActivity.this,"收藏失败",Toast.LENGTH_SHORT).show();
            }
            else{
                if(isCollect){
                    isCollect=false;
                    imageView3.setImageResource(R.drawable.collect3);
                    textView8.setText("收藏");
                    Toast.makeText(FoodInfoActivity.this,"取消收藏成功",Toast.LENGTH_SHORT).show();
                }
                else {
                    isCollect=true;
                    imageView3.setImageResource(R.drawable.collect4);
                    textView8.setText("取消收藏");
                    Toast.makeText(FoodInfoActivity.this,"收藏成功",Toast.LENGTH_SHORT).show();
                }
            }
        }
        @Override
        public void onFail(String mag) {
            Toast.makeText(FoodInfoActivity.this,mag,Toast.LENGTH_SHORT).show();
        }
    };

    private IsCollectListener isCollectListener=new IsCollectListener() {
        @Override
        public void onResponse(IsCollectBean isCollectBean) {
            if(isCollectBean.getCollected().equals("0")){
                isCollect=false;
                imageView3.setImageResource(R.drawable.collect3);
                textView8.setText("收藏");
            }
            else {
                isCollect=true;
                imageView3.setImageResource(R.drawable.collect4);
                textView8.setText("取消收藏");
            }
        }
        @Override
        public void onFail(String mag) {
            Toast.makeText(FoodInfoActivity.this,mag,Toast.LENGTH_SHORT).show();
        }
    };
}
